import pygame
import datetime
import sys
import os
# 初始化pygame
pygame.init()

# 初始窗口设置
WINDOWED_SIZE = (800, 400)
screen = pygame.display.set_mode(WINDOWED_SIZE, pygame.RESIZABLE)
# pygame.display.set_caption("数字时钟 - 按F切换全屏，ESC退出")
pygame.display.set_caption("")

# 全屏状态变量
is_fullscreen = False


# 定义颜色f
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 120, 255)
GRAY = (50, 50, 50)

# 字体设置
clock_font = pygame.font.Font(None, 150)
date_font = pygame.font.Font(None, 60)

path='/home/w-dingyu01@uos/dy外网/My_model/pygame测试/'

# 2025-09-19 16:27:30
# clock_font = pygame.font.Font('clock.ttf', 150)
# date_font = pygame.font.Font('像素.ttf', 60)

clock_font = pygame.font.Font(os.path.join(path, 'clock.ttf'), 150)
date_font = pygame.font.Font(os.path.join(path, '像素.ttf'), 60)



# 游戏主循环
running = True
clock = pygame.time.Clock()

while running:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:  # 按ESC键退出
                running = False
            elif event.key == pygame.K_f:  # 按F键切换全屏
                is_fullscreen = not is_fullscreen
                if is_fullscreen:
                    # 获取当前显示设备的信息，设置全屏模式
                    screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
                else:
                    # 返回窗口模式
                    screen = pygame.display.set_mode(WINDOWED_SIZE, pygame.RESIZABLE)
        elif event.type == pygame.VIDEORESIZE:  # 窗口大小调整事件
            if not is_fullscreen:
                WINDOWED_SIZE = event.size
                screen = pygame.display.set_mode(WINDOWED_SIZE, pygame.RESIZABLE)
    
    # 获取当前屏幕尺寸
    screen_width, screen_height = screen.get_size()
    
    # 根据屏幕尺寸动态调整字体大小
    clock_font_size = int(screen_height * 0.2)
    date_font_size = int(screen_height * 0.07)
    
    # 重新创建字体对象
    clock_font = pygame.font.Font(None, clock_font_size)
    date_font = pygame.font.Font(None, date_font_size)
    
    # clock_font = pygame.font.Font('clock.ttf', clock_font_size)
    # date_font = pygame.font.Font('像素.ttf', date_font_size)


    clock_font = pygame.font.Font(os.path.join(path, 'clock.ttf'), clock_font_size)
    date_font = pygame.font.Font(os.path.join(path, '像素.ttf'), date_font_size)



    # 填充背景色 - 使用渐变背景
    for y in range(screen_height):
        # 计算渐变颜色
        color_value = int(20 + (y / screen_height) * 10)
        screen.fill((color_value, color_value, color_value), (0, y, screen_width, 1))
    
    # 获取当前时间
    current_time = datetime.datetime.now()
    
    # 格式化时间显示
    time_str = current_time.strftime("%H:%M:%S")
    date_str = current_time.strftime("%Y-%m-%d %A")
    
    # 渲染时间文本
    time_text = clock_font.render(time_str, True, WHITE)
    date_text = date_font.render(date_str, True, BLUE)
    
    # 添加文本阴影效果
    time_shadow = clock_font.render(time_str, True, GRAY)
    date_shadow = date_font.render(date_str, True, GRAY)
    
    # 获取文本的矩形区域以便居中显示
    time_rect = time_text.get_rect(center=(screen_width/2, screen_height/2 - screen_height*0.1))
    date_rect = date_text.get_rect(center=(screen_width/2, screen_height/2 + screen_height*0.1))
    
    # 绘制阴影（偏移几个像素）
    screen.blit(time_shadow, (time_rect.x + 3, time_rect.y + 3))
    screen.blit(date_shadow, (date_rect.x + 2, date_rect.y + 2))
    
    # 绘制文本
    screen.blit(time_text, time_rect)
    screen.blit(date_text, date_rect)
    
    # 在窗口模式下显示提示文字
    if not is_fullscreen:
        help_font = pygame.font.Font(None, 20)
        # 字体设置
        
        import os

        # 检查是否存在中文字体文件
        font_path = "Ubuntu_18.04_SimHei.ttf"  # 假设字体文件在当前目录
        font_path=os.path.join(path, font_path)

        if os.path.exists(font_path):
            help_font = pygame.font.Font(font_path, 20)
        else:
            help_font = pygame.font.SysFont('sans', 20)  # fallback to a default font


        help_text = help_font.render("按 F 切换全屏，ESC 退出，可拖动调整窗口大小", True, (150, 150, 150))
        help_rect = help_text.get_rect(center=(screen_width/2, screen_height - 20))
        screen.blit(help_text, help_rect)
    
    # 更新显示
    pygame.display.flip()
    
    # 控制帧率，每秒更新10次就足够了
    clock.tick(10)

# 退出pygame
pygame.quit()
sys.exit()